package com.ruitu.mall_98_2.order.fragment;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.my_base.Config;
import com.beanu.arad.base.my_base.WineRefreshHeader;
import com.beanu.arad.base.my_base.base_list.ListBaseAdapter;
import com.beanu.arad.base.my_base.base_list.SuperViewHolder;
import com.beanu.arad.widget.dialog.CustomAlertDialog;
import com.bumptech.glide.Glide;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.base.BaseFragment;
import com.ruitu.mall_98_2.mine.AfterSaleActivity;
import com.ruitu.mall_98_2.mvp.contract.OrderContract;
import com.ruitu.mall_98_2.mvp.model.OrderModel;
import com.ruitu.mall_98_2.mvp.presenter.OrderPresenter;
import com.ruitu.mall_98_2.order.AfterSalesOrderActivity;
import com.ruitu.mall_98_2.order.CheckLogisticsActivity;
import com.ruitu.mall_98_2.order.OrderDetailsActivity;
import com.ruitu.router_module.A_Rutils;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.api.PageModel;
import com.ruitu.router_module.bean.order.Order;
import com.ruitu.router_module.bean.order.SmallOrder;

import java.util.List;

public class WaitingSendFragment extends BaseFragment<OrderPresenter, OrderModel> implements OrderContract.View, ListBaseAdapter.OnItemClickListener {
    private DataAdapter adapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private List<SmallOrder> smallOrder;
    private LRecyclerView lRecyclerView;
    private CustomAlertDialog customAlertDialog;

    public static WaitingSendFragment newInstance() {

        Bundle args = new Bundle();
        WaitingSendFragment fragment = new WaitingSendFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order, null);
        lRecyclerView = view.findViewById(R.id.lRecyclerView);
        lRecyclerView.setRefreshHeader(new WineRefreshHeader(getActivity()));
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        lRecyclerView.setLayoutManager(manager);
        adapter = new DataAdapter(getContext());
        lRecyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        lRecyclerView.setAdapter(lRecyclerViewAdapter);
        adapter.setOnItemClickListener(this);
        lRecyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                reReq();
            }
        });
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        reReq();
    }

    protected void reReq() {
//        showProgress();
        mPresenter.getOtherAllOrders(AppHolder.instance().getUser().getAccountId(), "2", "1", "1000");
    }

    @Override
    public void setData1(PageModel<SmallOrder> orders) {
        this.smallOrder = orders.getDataList();
        lRecyclerView.refreshComplete(20);
        adapter.setDataList(smallOrder);
        setData(isDataEmpty(smallOrder),lRecyclerView);
    }

    @Override
    public void setData(Order order) {

    }

    @Override
    public void setSuc() {
        reReq();
    }

    @Override
    public void setFail() {
        showError();
        lRecyclerView.refreshComplete(20);
    }

    @Override
    protected void getReData() {
        super.getReData();
//        showProgress();
        reReq();
    }

    @Override
    public void onItemClick(View itemView, int position) {

    }

    class DataAdapter extends ListBaseAdapter<SmallOrder> {

        public DataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_order;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_pay_model = holder.getView(R.id.tv_pay_model);
            ImageView iv_commodity = holder.getView(R.id.iv_commodity);
            ImageView iv_company_logo = holder.getView(R.id.iv_company_logo);
            ImageView iv_order_type = holder.getView(R.id.iv_order_type);
            TextView tv_commodity_name = holder.getView(R.id.tv_commodity_name);
            TextView tv_commodity_info = holder.getView(R.id.tv_commodity_info);
            TextView tv_commodity_price = holder.getView(R.id.tv_commodity_price);
            TextView tv_commodity_num = holder.getView(R.id.tv_commodity_num);
            TextView tv_pay_type_adj = holder.getView(R.id.tv_pay_type_adj);
            TextView tv_actually_paid = holder.getView(R.id.tv_actually_paid);
            TextView tv_expressage_total = holder.getView(R.id.tv_expressage_total);
            TextView tv_company_name = holder.getView(R.id.tv_company_name);
            TextView tv_application_for_drawback = holder.getView(R.id.tv_application_for_drawback);
            RelativeLayout rl_waiting_send = holder.getView(R.id.rl_waiting_send);
            TextView tv_waiting_reciver = holder.getView(R.id.tv_waiting_reciver);
            TextView tv_check_logistics = holder.getView(R.id.tv_check_logistics);
            LinearLayout ll_waiting_reciver = holder.getView(R.id.ll_waiting_reciver);
            LinearLayout ll_small_item = holder.getView(R.id.ll_small_item);
            final SmallOrder smallOrder = mDataList.get(position);
            SpannableString spannableString = new SpannableString("   " + smallOrder.getGoodsName());
            Drawable drawable = null;
            if (!TextUtils.isEmpty(smallOrder.getOrderType())) {
                switch (smallOrder.getOrderType()) {
                    case "3":
                        drawable = mContext.getResources().getDrawable(R.mipmap.collage);
                        break;
                    case "2":
                        drawable = mContext.getResources().getDrawable(R.mipmap.reserve);
                        break;
                    case "1":
                        drawable = mContext.getResources().getDrawable(R.mipmap.presale);
                        break;
                    default:

                        break;
                }
            }
            ImageSpan imageSpan = new ImageSpan(drawable);
            if (drawable == null) {
                tv_commodity_name.setText(smallOrder.getGoodsName());
            } else {
                drawable.setBounds(0, 0, 100, 50);
                spannableString.setSpan(imageSpan, 0, 2, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                tv_commodity_name.setText(spannableString);
            }

            tv_company_name.setText(smallOrder.getCompanyName());
            tv_pay_model.setText("待发货");
            Glide.with(mContext).load(Config.BASE_URL + smallOrder.getGoodsUrl()).apply(Arad.opts).into(iv_commodity);
            Glide.with(mContext).load(Config.BASE_URL + smallOrder.getCompanyLogo()).apply(Arad.opts_shop).into(iv_company_logo);
            tv_commodity_info.setText(smallOrder.getSkuContent());
            tv_commodity_num.setText("x" + smallOrder.getQuantity());
            tv_commodity_price.setText("￥" + smallOrder.getGoodsTotal());
            tv_pay_type_adj.setText("共" + smallOrder.getQuantity() + "件商品");
            tv_actually_paid.setText("实付款:" + smallOrder.getOrderTotal() + "元");
            tv_expressage_total.setText("(包含运费" + smallOrder.getFreight() + "元)");
            if (Double.parseDouble(smallOrder.getFreight()) > 0) {
                tv_expressage_total.setVisibility(View.VISIBLE);
            } else {
                tv_expressage_total.setVisibility(View.GONE);
            }
            final Bundle bundle = new Bundle();
            //申请退款
            tv_application_for_drawback.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    bundle.putSerializable("smallOrder", smallOrder);
                    startActivity(AfterSaleActivity.class, bundle);
                }
            });

            //item查看订单详情
            ll_small_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    bundle.putSerializable("smallOrder", smallOrder);
                    startActivity(OrderDetailsActivity.class, bundle);
                }
            });

            RelativeLayout rl_company = holder.getView(R.id.rl_company);
            tv_company_name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        bundle.putString("companyId", smallOrder.getCompanyId());
                        A_Rutils.startActivity(mContext, "com.ruitu.mall_98_1.product.activity.ShopDetailActivity", bundle);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

    }

}
